package net.root.plain.list;

import java.util.ArrayList;
import net.root.plain.alarm.AlarmDTO;
import net.root.plain.main.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class MultiAdapter extends BaseAdapter {
	LayoutInflater mInflater;
	ArrayList<ListItem> arSrc;
	ViewHolder viewHolder;
	AlarmDTO dto = new AlarmDTO();

	public MultiAdapter(Context context, ArrayList<ListItem> arItem) {
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		arSrc = arItem;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return arSrc.size();
	}

	@Override
	public ListItem getItem(int position) {
		// TODO Auto-generated method stub
		return arSrc.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return arSrc.get(position).getType();
	}

	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 3;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View v = convertView;
		if (v == null) {
			viewHolder = new ViewHolder();
			int res = 0;
			switch (arSrc.get(position).getType()) {
			case 0:
				res = R.layout.list_text;
				break;
			case 1:
				res = R.layout.list_check;
				break;
			case 2:
				res = R.layout.list_edittext;
			}
			v = mInflater.inflate(res, null);
		}
		switch (arSrc.get(position).getType()) {
		case 0:
			viewHolder.ltv = (TextView) convertView
					.findViewById(R.id.list_text_main);
			viewHolder.stv = (TextView) convertView
					.findViewById(R.id.list_text_sub);
			v.setTag(viewHolder);
			
			viewHolder.ltv.setText(arSrc.get(position).getLargeText());
			viewHolder.stv.setText(arSrc.get(position).getSmallText());
			viewHolder.ltv.setTag(position);
			viewHolder.stv.setTag(position);
			
			break;
		case 1:
			CheckBox cb = (CheckBox) convertView.findViewById(R.id.list_cBox);
			cb.setText(arSrc.get(position).getCBoxText());
			break;
		case 2:
			EditText edt = (EditText) convertView
					.findViewById(R.id.list_editText);
			edt.setHint(arSrc.get(position).getEditText());
			break;
		}
		return convertView;
	}
	
	public void setAlarmDTO(int position, String text){
		switch (position) {
		case 0:
			dto.setAlarmStatus(0);
			break;
		case 1:
			dto.setAlarmTime(text);
			break;
		case 2:
			dto.setAlarmRepeat(text);
			break;
		case 3:
			dto.setAlarmBell(text);
			break;
		case 4:
			dto.setAlarmVibrate(0);
			break;
		case 5:
			dto.setAlarmLabel(text);
			break;
		}
	}
	
	class ViewHolder{
		TextView ltv = null;
		TextView stv = null;
		EditText edt = null;
		CheckBox cb = null;
	}
	
	@Override
	protected void finalize() throws Throwable {
		// TODO Auto-generated method stub
		free();
		super.finalize();
	}
	
	public void free(){
		mInflater = null;
		arSrc = null;
		viewHolder = null;
	}
}
